package mbsl.ui.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

/** A set of simple blocking dialogs */
public final class Dialogs {
	/**
	 * Shows a simple input dialog.
	 * @param pParent The parent window.
	 * @param pMessage The message shown in the body of the dialog.
	 * @param pHeader The title of the dialog.
	 * @return The input given by the user, or an empty string if no input was given, or null if the dialog was cancelled.
	 */
	public static String input(Shell pParent, String pMessage, String pHeader) {
		Input input = new Input(pParent, pMessage, pHeader);
		
		return input.open();
	}
	/**
	 * Shows a simple error message.
	 * @param pParent The parent window.
	 * @param pMessage The message shown in the body of the dialog.
	 * @param pHeader The title of the dialog.
	 */
	public static void error(Shell pParent, String pMessage, String pHeader) {
		MessageBox message = new MessageBox(pParent, SWT.ICON_ERROR | SWT.OK);
		
		message.setText(pHeader);
		message.setMessage(pMessage);
		
		message.open();
	}
	/**
	 * Shows a simple question dialog.
	 * @param pParent The parent window.
	 * @param pMessage The message shown in the body of the dialog.
	 * @param pHeader The title of the dialog.
	 * @return The reply; one of SWT.YES, SWT.NO, or SWT.CANCEL.
	 */
	public static int question(Shell pParent, String pMessage, String pHeader) {
		MessageBox message = new MessageBox(pParent, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
		
		message.setText(pHeader);
		message.setMessage(pMessage);
		
		return message.open();
	}
	/**
	 * Shows a simple folder selection dialog.
	 * @param pParent The parent window.
	 * @param pMessage The message shown in the body of the dialog.
	 * @param pHeader The title of the dialog.
	 * @return A String holding the absolute path of the selected folder, or null if the dialog was cancelled.
	 */
	public static String folder(Shell pParent, String pMessage, String pHeader) {
		DirectoryDialog dialog = new DirectoryDialog(pParent);
		
		dialog.setText(pHeader);
		dialog.setMessage(pMessage);
		
		return dialog.open();
	}
	/**
	 * A simple selection dialog, with a single-selection list.
	 * @param pParent The parent window.
	 * @param pListData A {@link java.util.List List} of Objects.  The text is retrieved using toString().
	 * @param pMessage The message shown in the body of the dialog.
	 * @param pHeader The title of the dialog.
	 * @return The index of the selected element, or null if no item was selected or the dialog was cancelled.
	 */
	public static Integer selector(Shell pParent, java.util.List<?> pListData, String pMessage, String pHeader) {
		Selector selector = new Selector(pParent, pListData, pMessage, pHeader);
		
		return selector.open();
	}
	
	public static void main(String[] pArgs) {
		Display d = new Display();
		
		Shell s = new Shell(d, SWT.NONE);
		
		java.util.List<Object> l = new java.util.ArrayList<Object>();
		
		l.add(new Object() {
			public String toString() {
				return "Cake";
			}
		});
		
		System.out.println(Dialogs.selector(s, l, "Cake", "Love"));
		System.out.println(Dialogs.input(s, "Cake", "Omgz"));
		
		d.dispose();
	}
	
	private Dialogs() {}
}
